import { Util } from '../../utils/util.js';
import { CsFundDouble } from '../common/csFundDouble.js';
import { TargetObject, TargetObjectType } from '../targetOrg/targetObject.js';

export class TargetObjFundingAccount {
    constructor({
        tObj,
        /// 待收款
        pendingIncome = 0,
        /// 待付款(包含付款中部分)
        pendingPayment = 0,
        /// 账户余额
        balance = 0,
        /// 付款中费用
        payingAmount = 0,

        // 记录原始数据用于copy 转换
        totalAmount = 0,
        /// 已处理金额
        handledAmount = 0

    }) {
        this.tObj = tObj;
        this.pendingIncome = pendingIncome;
        this.pendingPayment = pendingPayment;
        this.balance = balance;
        this.payingAmount = payingAmount;
        this.totalAmount = totalAmount;
        this.handledAmount = handledAmount;

    }

    static fromJson(json) {
        const tObj = new TargetObject({
            id: json.objId || '',
            name: json.objName || '',
            objType: TargetObjectType.Unknown,
            contactName: json.contactUser || '',
            contactPhoneNumber: json.contactPhone || ''
        });

        const totalAmount = Util.jsonToDouble(json.sumPrice, 0);
        const payingAmount = Util.jsonToDouble(json.prePrice, 0);
        const handledAmount = Util.jsonToDouble(json.overPrice, 0);

        const pendingPayment = new CsFundDouble(
            totalAmount - (handledAmount - payingAmount)
        ).value;

        return new TargetObjFundingAccount({
            tObj,
            payingAmount,
            pendingPayment,
            pendingIncome: 0,
            balance: 0,
            totalAmount,
            handledAmount
        });
    }


    toCopyJson() {
        return {
            objId: this.tObj.id,
            objName: this.tObj.name,
            objType: this.tObj.objType.code,
            sumPrice: this.totalAmount.value,
            prePrice: this.payingAmount.value,
            overPrice: this.handledAmount.value,
        };
    }

    // 打印信息（调试用）
    toPrintInfoJson() {
        return {
            objId: this.tObj.id,
            objName: this.tObj.name,
            objType: this.tObj.objType,
            payingAmount: this.payingAmount,
            pendingIncome: this.pendingIncome,
            pendingPayment: this.pendingPayment,
            balance: this.balance
        };
    }
}
